defmodule TestHttpRequest do
	@moduledoc false

	require ProjectType
	require Logger

	# HTTPoison.post("http://localhost:4000/api/v1/lab/xz/access/insert", [],[], params: %{"token" => "yPAKX4u_BYOyVUH4OtHUrFIPpFXfjkjt"})
	@token :token

	def send(method \\ :get, action, params, _proj_type \\ ProjectType.proj_lab()) do
		token = params["token"]
		header = if token != nil do
			["authorization": params["token"]]
		else
			[]
		end
		args = case method do
			:get ->
				url = concat_url(get_ip_port(), action, params)
				[url, header, [{:hackney, [pool: :default]}]]
			:delete ->
				[post_url(get_ip_port(), action), [], [{:params, params}, {:hackney, [pool: :default]}]]
			_ -> # :post
				[post_url(get_ip_port(), action), [], header, [{:params, params}, {:hackney, [pool: :default]}]]
		end
		try do
			apply(HTTPoison, method, args)
			|> parse_resp()
		rescue
			err ->
				Logger.error(inspect({err, __STACKTRACE__}))
				err
		catch
			err ->
				Logger.error("Resp Error, #{inspect(err)}")
				err
		end
	end

	def upload_file(action, img_type, file, params) do
		filename = Path.basename(file)
		proj_type = get_login_args()["project_type"]
		_list = [
			{:file, file, {"form-data", [name: img_type <> "_1", filename: filename]}, []},
			{:file, file, {"form-data", [name: img_type <> "_2", filename: filename]}, []},
		]

		list = [
			{:file, file, img_type <> "_1", []},
			{:file, file, img_type <> "_2", []},
		]
		#		data = {:data, "file", list, []}
		#		[
		#			{:file, file},
		#			{"name", img_type <> "_1"},
		#		]
		HTTPoison.post(
			post_url(get_ip_port(), proj_type <> "/" <> action),
			{:multipart, list ++ Enum.to_list(params)},
			[],
			[{:params, params}]
		)

		#		HTTPoison.post(
		#			post_url(get_ip_port(), proj_type <> "/" <> action),
		#			{:multipart, list},
		#			[{"Content-Type", "multipart/form-data"}],
		#			[{:params, params}]
		#		)

		#		|> parse_resp()
	end

	def upload_pdf(action, file, params) do
		filename = Path.basename(file)
		proj_type = get_login_args()["project_type"]
		HTTPoison.post(
			post_url(get_ip_port(), proj_type <> "/" <> action),
			{:multipart, [{:file, file, {"form-data", [name: "filepdf", filename: filename]}, []}]},
			[],
			[{:params, params}]
		)
		|> parse_resp()
	end

	def parse_resp({:ok, resp}) do
		status_code = resp.status_code
		if status_code == 200 do
			body = resp.body
			Jason.decode!(body)
		else
			if status_code == 422 do
				Process.delete(:user)
				get_login_args()
				raise TipError, "not_login"
			else
				throw({:error, "Status Code Error", status_code, resp})
			end
		end
	end
	def parse_resp({:error, resp}) do
		Logger.info(inspect(resp))
		%{}
	end
	def parse_resp(resp) do
		Logger.info(inspect(resp.body))
		%{}
	end

	@pages :lists.seq(1, 10)
	def random_page() do
		Random.random_one(@pages)
	end

	def random_bool() do
		Random.random_one([true, false])
	end


	def get_value(data, cmd_key, key) do
		get_data(data)
		|> Map.get(cmd_key)
		|> MapLib.get_by_key(key)
	end

	def get_data({:ok, body}) do
		get_data(body)
	end
	def get_data(body) do
		Map.get(body, "data")
	end

	def url() do
		#		"http://39.100.229.159:4007/api/v1/"
		#		"http://192.168.9.248:4007/api/v1/"
		#		"http://www.zxserver.com/api/v1/"
		#		"http://192.168.31.189:4007/api/v1/"
		"http://192.168.31.15:4007/api/v1/"
	end

	def concat_url(ip_port, action, params) do
		(ip_port <> action <> "?") <> Enum.join(Enum.map(params, fn ({k, v}) -> "#{k}=#{v}" end), "&")
	end

	def post_url(ip_port, action) do
		ip_port <> action
	end

	def deepr_object(mod) do
		Macro.underscore(mod)
		|> String.replace("_test", "")
	end

	def get_login_args() do
		mod = Process.get(:test_user_mod)
		mod != nil || throw({:error, "请先登录", mod})
		mod.get_login_args()
	end

	# 18582883104 15882267723, super_admin
	def phone() do
		#		"49480397224"
		#		"15882267723"
		#		"super_admin"
		"18582883104"
	end
	def passwd() do
		#		"000000"
		"123456"
	end


	def put_token(token) do
		Process.put(@token, token)
	end

	defp get_ip_port() do
		ip_port = get_login_args()["ip_port"]
		ip_port || throw({:error, "IpPort Is Null, 登录参数:", get_login_args()})
	end

end
